package com.nanxhs.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * cglib代理class
 *
 * @author: haibin.tang
 * @date: 2021/2/5
 */
public class CglibMethodInterceptor implements MethodInterceptor {

    private Object originObj;

    public CglibMethodInterceptor(Object originObj) {
        this.originObj = originObj;
    }

    public Object getProxy(Class superClass) {
        Enhancer enhancer = new Enhancer();
        enhancer.setCallback(this);
        enhancer.setSuperclass(superClass);
        return enhancer.create();
    }

    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.out.println("调用开始" + obj.getClass().getName() + "#" + method.getName());
        return proxy.invokeSuper(obj, args);
//        return proxy.invoke(originObj, args);
    }
}
